<?
	 layout("/inc/win_layout.html",{title:sysTitle}){
    ?>
<script type="text/javascript"
	src="static/My97DatePicker/WdatePicker.js"></script>

<div class="panel" style="height: 610px; overflow: auto;">
	<div class="panel-body pan">
		<div class="row" style="margin-bottom: 10px;">
			<div class="col-lg-6"
				style="width: 400px; float:left; margin-right:-20px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">用户名称</label>
				<div class="col-md-9">
					 <div class="input-icon">
						<input id="organizationUsername" type="text" disabled="true" class="form-control" value="" />
						<input id="organizationUserid" type="hidden" class="form-control" value="" />
					</div>
				</div>
			</div>
			<div class="col-lg-6"
				style="width: 400px; float:left; margin-right:-20px; display:inline;">
				<label for="inputUsername" class="col-md-3 control-label">项目名称
					<span class='require'>*</span>
				</label>
				<div class="col-md-9">
					<div class="input-icon">
						<select class="form-control" id="projectId" name="projectName">
							<!--<option value=''>请先选择用户</option>
							<?for(project in projectList){?>
							<option value="${project.type}" table="${project.id}"nameZh="${project.nameZh}">${project.nameZh}</option>
							<?}?> -->
						</select>
					</div>
				</div>
			</div>
		</div>
		<div class="row" style="margin-bottom: 10px;height:750px;"
			id="multplediv"></div>
	</div>
</div>
<?} ?>
<script>
	var sumbitId = window.parent.getsumbitId();
	var sumbitNameZh = window.parent.getSumbitNameZh();
	$("#organizationUsername").val(sumbitNameZh);
	$("#organizationUserid").val(sumbitId);
	$(function() {
		queryJg(sumbitId);
	});
	//动态构建
	var balance = '';
	var time = '';
	var count = '';
	$(document).ready(function() {
			$("#projectId").bind("change",function() {
				var project = $("#projectId").find(
						"option:selected").attr(
						"table");
				var nameZh = $("#projectId").find(
						"option:selected").attr(
						"nameZh");
				var text = "";
				if ($(this).val() == 'balance'
						&& balance == '') {
					balance = $(this).val();
					text += "<div class='col-lg-12' id='balanceDiv' ><input id='balance_project' type='hidden' value='"+project+"' class='form-control' /><div class='panel '><div class='panel-heading'></div><div class='panel-body pan'><h4 style='line-height: 35px;padding-right: 15px;'>&nbsp;&nbsp;"
							+ nameZh
							+ "（"
							+ $(this).val()
							+ "）<a href='javascript:deleteDiv(\"balance\");' class='btn btn-danger' style='float: right;'> 删除 </a></h4>";
					text += "<div class='col-lg-6' style='width: 400px; float:left; margin-right:-20px;display:inline;'>"
							+ "<label for='inputUsername' class='col-md-3 control-label'>生效时间<span class='require'>*</span> </label>"
							+ "<div class='col-md-9'><div class='input-icon'>"
							+ "<input id='balance_start' type='text' class='form-control' value='' onchange=\"dateJudge('"+this.value+"','"+project+"')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
							+ "</div></div></div>";
					text += "<div class='col-lg-4' style='width: 400px; float:left; margin-right:-20px;display:inline;'>"
							+ "<label for='inputUsername' class='col-md-3 control-label'>失效时间<span class='require'>*</span> </label>"
							+ "<div class='col-md-9'><div class='input-icon'>"
							+ "<input id='balance_end' type='text' class='form-control' value='' onchange=\"timeJudge('"+this.value+"')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
							+ "</div></div></div>";
					text += "<div class='col-lg-6' style='width: 400px; float:left; margin-right:-20px;display:inline;'>"
							+ "<label for='inputUsername' class='col-md-3 control-label'>余额<span class='require'>*</span> </label>"
							+ "<div class='col-md-9'><div class='input-icon'>"
							+ "<input id='balance_balance' type='text' class='form-control' value='' />"
							+ "</div></div></div>";
					text += "<div class='col-lg-12' style='width: 800px; float:left; margin-right:-20px;display:inline;'>"
					text += "<label for='inputUsername' class='col-md-3 control-label'>产品库<span class='require'></span> </label>"
					text += "<div class='col-md-9'><div class='input-icon'><ul>";
					$.ajax({
								type : "post",
								url : "organizationUserProduct.do",
								data : {
									"projectId" : project
								},
								async : false,
								success : function(
										data) {
									for ( var i in data) {
										var name = data[i].name;
										text += "<li style='float:left;margin-right:30px;list-style-type:none;'><input id='balance"+data[i].id+"' name='balance' type='checkbox' value='"+data[i].id+"'/>&nbsp;&nbsp;<label for='balance"+data[i].id+"'>"
												+ name
												+ "</label></li>";
									}
								}
							});
					text += "</ul></div></div></div>";

					text += "</div></div></div></div>";
				}
				if ($(this).val() == 'time'
						&& time == '') {
					time = $(this).val();
					text += "<div class='col-lg-12' id='timeDiv' ><input id='time_project' type='hidden' value='"+project+"' class='form-control' /><div class='panel '><div class='panel-heading'></div><div class='panel-body pan'><h4 style='line-height: 35px;padding-right: 15px;'>&nbsp;&nbsp;"
							+ nameZh
							+ "（"
							+ $(this).val()
							+ "）<a href='javascript:deleteDiv(\"time\");' class='btn btn-danger' style='float: right;'> 删除 </a></h4>";
					text += "<div class='col-lg-6' style='width: 400px; float:left; margin-right:-20px;display:inline;'>"
							+ "<label for='inputUsername' class='col-md-3 control-label'>生效时间<span class='require'>*</span> </label>"
							+ "<div class='col-md-9'><div class='input-icon'>"
							+ "<input id='time_start' type='text' class='form-control' value='' onchange=\"dateJudge('"+this.value+"','"+project+"')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
							+ "</div></div></div>";
					text += "<div class='col-lg-4' style='width: 400px; float:left; margin-right:-20px;display:inline;'>"
							+ "<label for='inputUsername' class='col-md-3 control-label'>失效时间<span class='require'>*</span> </label>"
							+ "<div class='col-md-9'><div class='input-icon'>"
							+ "<input id='time_end' type='text' class='form-control' value='' onchange=\"timeJudge('"+this.value+"')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
							+ "</div></div></div>";
					text += "<div class='col-lg-12' style='width: 800px; float:left; margin-right:-20px;display:inline;'>"
					text += "<label for='inputUsername' class='col-md-3 control-label'>产品库<span class='require'></span> </label>"
					text += "<div class='col-md-9'><div class='input-icon'><ul>";
					$.ajax({
								type : "post",
								url : "organizationUserProduct.do",
								data : {
									"projectId" : project
								},
								async : false,
								success : function(
										data) {
									for ( var i in data) {
										var name = data[i].name;
										text += "<li style='float:left;margin-right:30px;list-style-type:none;'><input id='time"+data[i].id+"' name='time' type='checkbox' value='"+data[i].id+"'/>&nbsp;&nbsp;<label for='time"+data[i].id+"'>"
												+ name
												+ "</label></li>";
									}
								}
							});
					text += "</ul></div></div></div>";

					text += "</div></div></div></div>";
				}
				if ($(this).val() == 'count'
						&& count == '') {
					count = $(this).val();
					text += "<div class='col-lg-12' id='countDiv'><input id='count_project' type='hidden' value='"+project+"' class='form-control' /><div class='panel '><div class='panel-heading'></div><div class='panel-body pan'><h4 style='line-height: 35px;padding-right: 15px;'>&nbsp;&nbsp;"
							+ nameZh
							+ "（"
							+ $(this).val()
							+ "）<a href='javascript:deleteDiv(\"count\");' class='btn btn-danger' style='float: right;'> 删除 </a></h4>";
					text += "<div class='col-lg-6' style='width: 400px; float:left; margin-right:-20px;display:inline;'>"
							+ "<label for='inputUsername' class='col-md-3 control-label'>生效时间<span class='require'>*</span> </label>"
							+ "<div class='col-md-9'><div class='input-icon'>"
							+ "<input id='count_start' type='text' class='form-control' value='' onchange=\"dateJudge('"+this.value+"','"+project+"')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
							+ "</div></div></div>";
					text += "<div class='col-lg-4' style='width: 400px; float:left; margin-right:-20px;display:inline;'>"
							+ "<label for='inputUsername' class='col-md-3 control-label'>失效时间<span class='require'>*</span> </label>"
							+ "<div class='col-md-9'><div class='input-icon'>"
							+ "<input id='count_end' type='text' class='form-control' value='' onchange=\"timeJudge('"+this.value+"')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
							+ "</div></div></div>";
					text += "<div class='col-lg-6' style='width: 400px; float:left; margin-right:-20px;display:inline;'>"
							+ "<label for='inputUsername' class='col-md-3 control-label'>剩余次数<span class='require'>*</span> </label>"
							+ "<div class='col-md-9'><div class='input-icon'>"
							+ "<input id='count_surplus' type='text' class='form-control' value='' />"
							+ "</div></div></div>";
				/* 	text += "<div class='col-lg-6' style='width: 400px; float:left; margin-right:-20px;display:inline;'>"
							+ "<label for='inputUsername' class='col-md-3 control-label'>累计消费次数<span class='require'>*</span> </label>"
							+ "<div class='col-md-9'><div class='input-icon'>"
							+ "<input id='count_accumulative' type='text' class='form-control' value='' />"
							+ "</div></div></div>"; */
					text += "<div class='col-lg-12' style='width: 800px; float:left; margin-right:-20px;display:inline;'>"
					text += "<label for='inputUsername' class='col-md-3 control-label'>产品库<span class='require'></span> </label>"
					text += "<div class='col-md-9'><div class='input-icon'><ul>";
					$.ajax({
								type : "post",
								url : "organizationUserProduct.do",
								data : {
									"projectId" : project
								},
								async : false,
								success : function(
										data) {
									for ( var i in data) {
										var name = data[i].name;
										text += "<li style='float:left;margin-right:30px;list-style-type:none;'><input id='count"+data[i].id+"' name='count' type='checkbox' value='"+data[i].id+"'/>&nbsp;&nbsp;<label for='count"+data[i].id+"'>"
												+ name
												+ "</label></li>";
									}
								}
							});
					text += "</ul></div></div></div>";

					text += "</div></div></div></div>";
				}
				$("#multplediv").prepend(text);
			});
		});

	//添加
	function save() {
		/**balance statr*/
		var balanceMap = {};
		if (balance != '') {
			var balance_project = [];
			$("input[name='balance']:checked").each(function() {
				balance_project.push($(this).val());
			});
			var balanceProject = '';
			for ( var i = 0; i < balance_project.length; i++) {
				balanceProject += balance_project[i] + ",";
			}
			if ($.trim($("#balance_start").val()).length < 1) {
				alert('生效时间不能为空！');
				return null;
			}
			if ($.trim($("#balance_end").val()).length < 1) {
				alert('失效时间不能为空！');
				return null;
			}
			if ($.trim($("#balance_balance").val()).length < 1) {
				alert('余额不能为空！');
				return null;
			}
			if ($.trim(balanceProject).length < 1) {
				alert('产品至少选择一个！');
				return null;
			}
			balanceMap["project"] = balanceProject;
			balanceMap["start"] = $("#balance_start").val();
			balanceMap["end"] = $("#balance_end").val();
			balanceMap["balance"] = $("#balance_balance").val();
			balanceMap["projectId"] = $("#balance_project").val();
			balanceMap["organizationUserid"] = $("#organizationUserid").val();
		}
		/**balance end*/

		/**time statr*/
		var timeMap = {};
		if (time != '') {
			var time_project = [];
			$("input[name='time']:checked").each(function() {
				time_project.push($(this).val());
			});
			var timeProject = '';
			for ( var i = 0; i < time_project.length; i++) {
				timeProject += time_project[i] + ",";
			}
			if ($.trim($("#time_start").val()).length < 1) {
				alert('生效时间不能为空！');
				return null;
			}
			if ($.trim($("#time_end").val()).length < 1) {
				alert('失效时间不能为空！');
				return null;
			}
			if ($.trim(timeProject).length < 1) {
				alert('产品至少选择一个！');
				return null;
			}
			timeMap["project"] = timeProject;
			timeMap["start"] = $("#time_start").val();
			timeMap["end"] = $("#time_end").val();
			timeMap["projectId"] = $("#time_project").val();
			timeMap["organizationUserid"] = $("#organizationUserid").val();
		}
		/**time end*/

		/**count statr*/
		var countMap = {};
		if (count != '') {
			var count_project = [];
			$("input[name='count']:checked").each(function() {
				count_project.push($(this).val());
			});
			var countProject = '';
			for ( var i = 0; i < count_project.length; i++) {
				countProject += count_project[i] + ",";
			}
			if ($.trim($("#count_start").val()).length < 1) {
				alert('生效时间不能为空！');
				return null;
			}
			if ($.trim($("#count_end").val()).length < 1) {
				alert('失效时间不能为空！');
				return null;
			}
			if ($.trim($("#count_surplus").val()).length < 1) {
				alert('剩余次数为空！');
				return null;
			}
			if ($.trim(countProject).length < 1) {
				alert('产品至少选择一个！');
				return null;
			}
			countMap["project"] = countProject;
			countMap["start"] = $("#count_start").val();
			countMap["end"] = $("#count_end").val();
			countMap["surplus"] = $("#count_surplus").val();
			countMap["accumulative"] = '0';
			countMap["projectId"] = $("#count_project").val();
			countMap["organizationUserid"] = $("#organizationUserid").val();
		}
		/**count end*/
		$.ajax({
			type : "post",
			url : "organizationUserAdd.do",
			data : {
				"balanceMap" : JSON.stringify(balanceMap),
				"timeMap" : JSON.stringify(timeMap),
				"countMap" : JSON.stringify(countMap),
				"member":$("#organizationUserid").val()
			},
			dataType : "json",
			async : false,
			traditional : true,
			success : function(data) {
				var index = parent.layer.getFrameIndex(window.name);
				window.parent.demo(1);
				;
				parent.layer.msg('新增成功');
				parent.layer.close(index);
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	}
	//查询项目
	 function queryJg(jgId){
		$.ajax({
			type : "post",
			url : "queryJg.do",
			data : {
				"id" : jgId,
			},
			async : false,
			dataType : "json",
			success : function(data) {
				var src='<option value=" ">--请选择--</option>';
				for(var i=0; i<data.length;i++){
					src += '<option value="'+data[i].type+'" table="'+data[i].id+'" nameZh="'+data[i].nameZh+'" >'+data[i].nameZh+'</option>';
				}
				$("#projectId").html(src);
			},
			error : function(data) {
				alert("请求失败");
			}
		});
			
	 }
	 
	 //判断时间范围
	 function dateJudge(judgeType,dateProjectId){
	 	var startTimes = $("#"+judgeType+"_start").val();
	 	var mendId = $("#organizationUserid").val();
	 	$.ajax({
			type : "post",
			url : "goDateJudge.do",
			data : {
				"startTimes" : startTimes,
				"mendId" : mendId,
				"dateProjectId" : dateProjectId,
			},
			async : false,
			dataType : "json",
			success : function(data) {
				if(data[0].judge == 'no'){
					alert('该时间段已订购！请重新选择...');
					$("#"+judgeType+"_start").val("");
				}
				if(data[0].judge == 'noNewTime'){
					alert('无法选择已过时间！请重新选择...');
					$("#"+judgeType+"_start").val("");
				}
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	 }
	 //判断时间大小
	 function timeJudge(judgeType){
	 	var startTimes = $("#"+judgeType+"_start").val();
	 	if ($.trim(startTimes).length < 1) {
				alert('请先选择生效时间！');
				$("#"+judgeType+"_end").val("");
				return null;
		}
	 	var endTimes = $("#"+judgeType+"_end").val();
	 	$.ajax({
			type : "post",
			url : "goTimeJudge.do",
			data : {
				"startTimes" : startTimes,
				"endTimes" : endTimes,
			},
			async : false,
			dataType : "json",
			success : function(data) {
				if(data[0].judge == 'no'){
					alert('失效时间必须大于生效时间！');
					$("#"+judgeType+"_end").val("");
				}
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	 }
	  function deleteDiv(delType){
	 	$("#"+delType+"Div").remove();
	 	if(delType == 'balance'){
	 		balance='';
	 	}
	 	if(delType == 'time'){
	 		time='';
	 	}
	 	if(delType == 'count'){
	 		count='';
	 	}
	 }
</script>